package com.aio.kgo.simple.form.design.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aio.kgo.simple.form.common.domain.save.ModelDefinitionDo;
import com.aio.kgo.simple.form.database.domain.params.QueryModelParams;
import com.aio.kgo.simple.form.database.service.AioModelService;
import com.kgo.flow.common.domain.amis.AmisOptions;
import com.kgo.flow.common.domain.amis.AmisResult;
import com.kgo.flow.common.domain.page.KgoPage;
import com.kgo.flow.common.domain.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lizhenming
 * @desc: 模型维护接口
 * @date 2023/12/25 23:04
 */
@RestController
@RequestMapping("/simple/model/ops")
public class ModelOpsController {
    @Autowired
    private AioModelService modelService;
    @PostMapping
    public AmisResult createModel(@RequestBody ModelDefinitionDo definitionDo){
        ModelDefinitionDo model = modelService.createModel(definitionDo);
        return AmisResult.success(model);
    }
    @PutMapping
    public AmisResult updateModel(@RequestBody ModelDefinitionDo definitionDo){
        ModelDefinitionDo model = modelService.updateModel(definitionDo);
        return AmisResult.success(model);
    }
    @GetMapping
    public AmisResult getModelById(@RequestParam  String modelId){
        ModelDefinitionDo model = modelService.getModelById(modelId);
        return AmisResult.success(model);
    }
    @GetMapping("definition")
    public AmisResult getModelDefinitionById(@RequestParam  String modelId){
        ModelDefinitionDo model = modelService.getModelById(modelId);
        return ObjectUtil.isEmpty(model) ? AmisResult.success() : AmisResult.success(model.getModelDesign());
    }
    @GetMapping("page")
    public AmisResult getModelPage(@ModelAttribute QueryModelParams params,@ModelAttribute KgoPage page){
        PageResult<ModelDefinitionDo> pageResult = modelService.getModelPage(params,page);
        return AmisResult.success(pageResult);
    }
    @GetMapping("options")
    public AmisResult getModelPage(@RequestParam(required = false) String label){
        QueryModelParams params = new QueryModelParams();
        params.setModelName(label);
        KgoPage page = new KgoPage();
        page.setPageSize(100);
        PageResult<ModelDefinitionDo> pageResult = modelService.getModelPage(params,page);
        List<ModelDefinitionDo> modelDefList = pageResult.getList();
        AmisOptions options = new AmisOptions();
        if (ObjectUtil.isNull(modelDefList) || modelDefList.isEmpty()){
            return  AmisResult.success(options);
        }
        for (ModelDefinitionDo modelDefinitionDo : modelDefList) {
            options.addOption(modelDefinitionDo.getModelName(),modelDefinitionDo.getId());
        }
        return AmisResult.success(options);
    }
    @GetMapping("{module}/options")
    public AmisResult getModelOptions(@PathVariable String module,@RequestParam(required = false) String label){
        QueryModelParams params = new QueryModelParams();
        params.setModelName(label);
        params.setModule(module);
        KgoPage page = new KgoPage();
        page.setPageSize(100);
        PageResult<ModelDefinitionDo> pageResult = modelService.getModelPage(params,page);
        List<ModelDefinitionDo> modelDefList = pageResult.getList();
        AmisOptions options = new AmisOptions();
        if (ObjectUtil.isNull(modelDefList) || modelDefList.isEmpty()){
            return  AmisResult.success(options);
        }
        for (ModelDefinitionDo modelDefinitionDo : modelDefList) {
            options.addOption(modelDefinitionDo.getModelName(),modelDefinitionDo.getId());
        }
        return AmisResult.success(options);
    }


}

